package dao;

import pojo.Receiver;
import utils.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ReceiverDAOImpl {

    private static final String TABLE_NAME = "Receiver";

    public static void add(Receiver receiver) throws SQLException {
        String sql = "INSERT INTO " + TABLE_NAME + " VALUES (?, ?, ?)";
        JDBCUtils.execute(sql, null, receiver.getReceiverName(), receiver.getReceiverAddress());
    }

    public static void delete(int receiverID) throws SQLException {
        String sql = "DELETE FROM " + TABLE_NAME + " WHERE receiver_id = ?";
        JDBCUtils.execute(sql, receiverID);
    }

    public static void update(Receiver receiver) throws SQLException {
        String sql = "UPDATE " + TABLE_NAME + " SET receiver_name = ?, receiver_address = ? WHERE receiver_id = ?";
        JDBCUtils.execute(sql, receiver.getReceiverName(), receiver.getReceiverAddress(), receiver.getReceiverID());
    }

    public static Receiver getById(int receiverID) throws SQLException {
        String sql = "SELECT * FROM " + TABLE_NAME + " WHERE receiver_id = ?";
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql, receiverID);
        if (resultSet.next()) {
            return createReceiverFromResultSet(resultSet);
        }
        return null;
    }

    public static List<Receiver> getAll() throws SQLException {
        List<Receiver> receivers = new ArrayList<>();
        String sql = "SELECT * FROM " + TABLE_NAME;
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql);
        while (resultSet.next()) {
            Receiver receiver = createReceiverFromResultSet(resultSet);
            receivers.add(receiver);
        }
        return receivers;
    }

    public static int getCount() throws SQLException {
        String sql = "SELECT COUNT(*) AS count FROM " + TABLE_NAME;
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql);
        if (resultSet.next()) {
            return resultSet.getInt("count");
        }
        return 0;
    }

    public static List<Receiver> getByPage(int start, int pageSize) throws SQLException {
        List<Receiver> receivers = new ArrayList<>();
        String sql = "SELECT * FROM " + TABLE_NAME + " LIMIT ?, ?";
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql, start, pageSize);
        while (resultSet.next()) {
            Receiver receiver = createReceiverFromResultSet(resultSet);
            receivers.add(receiver);
        }
        return receivers;
    }

    public static List<Receiver> findByKeyword(String keyword) throws SQLException {
        List<Receiver> receivers = new ArrayList<>();
        String sql = "SELECT * FROM " + TABLE_NAME + " WHERE receiver_name LIKE ?";
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql, "%" + keyword + "%");
        while (resultSet.next()) {
            Receiver receiver = createReceiverFromResultSet(resultSet);
            receivers.add(receiver);
        }
        return receivers;
    }

    public static List<Receiver> findByPageAndKeyword(int start, int pageSize, String keyword) throws SQLException {
        List<Receiver> receivers = new ArrayList<>();
        String sql = "SELECT * FROM " + TABLE_NAME + " WHERE receiver_name LIKE ? LIMIT ?, ?";
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql, "%" + keyword + "%", start, pageSize);
        while (resultSet.next()) {
            Receiver receiver = createReceiverFromResultSet(resultSet);
            receivers.add(receiver);
        }
        return receivers;
    }

    private static Receiver createReceiverFromResultSet(ResultSet resultSet) throws SQLException {
        int receiverID = resultSet.getInt("receiver_id");
        String receiverName = resultSet.getString("receiver_name");
        String receiverAddress = resultSet.getString("receiver_address");
        return new Receiver(receiverID, receiverName, receiverAddress);
    }
}
